package com.xiaoxu.mapreduce.userAgent;

import com.kumkee.userAgent.UserAgent;
import com.kumkee.userAgent.UserAgentParser;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class User {

    @Test
    public void testOne(){
        String source="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36";
        UserAgentParser userAgentParser =new UserAgentParser();
        UserAgent agent=userAgentParser.parse(source);
        boolean mobile=agent.isMobile();
        String browser=agent.getBrowser();
        String platform=agent.getPlatform();
        String os=agent.getOs();
        String engine=agent.getEngine();
        String version=agent.getVersion();
        String engineVersion=agent.getEngineVersion();

        System.out.println(browser+" "+platform+" "+os+" "+engine+" "+" "+version+" "+engineVersion+" ");

    }
    private  int getCharPosition(String  str,String operator,int num){
        Matcher matcher = Pattern.compile(operator).matcher(str);
        int count=0;
        while (matcher.find()){
            count++;
            if (count == num){
                break;
            }
        }
        return  matcher.start();
    }
    @Test
    public  void  testGetCharPosition(){
        String  str="183.162.52.7 - - [10/Nov/2016:00:01:02 +0800] \\\"POST /api3/getadv HTTP/1.1\\\" 200 813 \\\"www.imooc.com\\\" \" +\n" +
                "                \"\\\"-\\\" cid=0&timestamp=1478707261865&uid=2871142&marking=androidbanner&secrect=\" +\n" +
                "                \"a6e8e14701ffe9f6063934780d9e2e6d&token=f51e97d1cb1a9caac669ea8acc162b96 \\\"mukewang\" +\n" +
                "                \"/5.0.0 (Android 5.1.1; Xiaomi Redmi 3 Build/LMY47V),Network 2G/3G\\\" \\\"-\\\" 10.100.134.244:80 200 0.027 0.027\\n\" +\n" +
                "                \"10.100.0.1 - - [10/Nov/2016:00:01:02 +0800] \\\"HEAD / HTTP/1.1\\\" 301 0 \\\"117.121.101.40\\\" \\\"-\\\" - \\\"curl/7.19.7 \" +\n" +
                "                \"(x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2\\\" \\\"-\\\" - - - 0.000";
        System.out.println(getCharPosition(str,"\"",7));
    }
    @Test
    public void testReadFile()throws  Exception{
        String path="src\\data\\10000_access.log";
        File file =new File(path);
        FileInputStream fileInputStream=new FileInputStream(file);
        InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream);
        BufferedReader reader =new BufferedReader(inputStreamReader);
        Map<String,Integer> browserMap=new HashMap<>();
        String line="";
        while (line!=null){
       line =reader.readLine();
       if (!StringUtils.isBlank(line)){
           int index =getCharPosition(line,"\"",7);
           String source=line.substring(index+1);
           UserAgentParser userAgentParser=new UserAgentParser();
           UserAgent agent =userAgentParser.parse(source);
           String browser=agent.getBrowser();
           Integer browserCount=browserMap.get(browser);
           if (browserCount !=null){
               browserMap.put(browser,browserCount+1);
           }else {
               browserMap.put(browser,1);
           }
       }
        }
        for (Map.Entry<String ,Integer>entry:browserMap.entrySet()){
            System.out.println(entry.getKey()+"-->"+entry.getKey());
        }
    }
}
